{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "IPython.notebook.set_autosave_interval(60000)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Autosaving every 60 seconds\n"
     ]
    }
   ],
   "source": [
    "%autosave 60\n",
    "import os\n",
    "import sys\n",
    "import math\n",
    "import string\n",
    "\n",
    "import pprint\n",
    "import shelve\n",
    "import shutil\n",
    "import send2trash\n",
    "import zipfile\n",
    "import traceback\n",
    "\n",
    "from dir_objectName import dir_objectName\n",
    "\n",
    "import logging\n",
    "logging.basicConfig(level=logging.INFO,\n",
    "                    format=\" %(asctime)s - %(levelname)s - %(message)s \")\n",
    "#!/Users/mach711/myproject/venv_python3/bin/python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多重剪贴板"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假定你有一个无聊的任务，要填充一个网页或软件中的许多表格，其中包含一些文本字段。\n",
    "\n",
    "剪贴板让你不必一次又一次输入同样的文本，但剪贴板上一次只有一个内容。\n",
    "\n",
    "如果你有几段不同的文本需要拷贝粘贴，就不得不一次又一次的标记和拷贝几个同样的内容。\n",
    "\n",
    "可以编写一个 Python 程序，追踪几段文本\n",
    "\n",
    "利用一个关键字保存每段剪贴板文本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<strong>程序要做的事</strong>：\n",
    "\n",
    "• 针对要检查的关键字，提供命令行参数。\n",
    "\n",
    "• 如果参数是 save，那么将剪贴板的内容保存到关键字。\n",
    "\n",
    "• 如果参数是 list，就将所有的关键字拷贝到剪贴板。\n",
    "\n",
    "• 否则，就将关键词对应的文本拷贝到剪贴板。\n",
    "\n",
    "\n",
    "<strong>这意味着代码需要做下列事情</strong>：\n",
    "\n",
    "• 从 sys.argv 读取命令行参数。\n",
    "\n",
    "• 读写剪贴板。\n",
    "\n",
    "• 保存并加载 shelf 文件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mcbShelf[sys.argv[2]] =  ['1234', 'abcd', 'one time one thing']\n",
      "Save clipboard content:Succes!\n",
      "\u001b[31m['1234', 'abcd', 'one time one thing']\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "#!/Users/mach711/myproject/venv_python3/bin/python\n",
    "# multiclipboardcb.pyw - Saves and loads pieces of text to the clipboard.\n",
    "# Usage: py.exe multiclipboard.pyw save <keyword> - Saves clipboard to keyword.\n",
    "#        py.exe mcb.pyw <keyword> - Loads keyword to clipboard.\n",
    "#        py.exe mcb.pyw list - Loads all keywords to clipboard.\n",
    "import shelve\n",
    "import pyperclip\n",
    "import sys\n",
    "# save clipboard content to shelve\n",
    "mcbShelf = shelve.open('mcb')\n",
    "sys.argv[1: 3] = ['save', 'one time one thing']\n",
    "# Save clipboard content.\n",
    "if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':\n",
    "    mcbShelf[sys.argv[2]] = pyperclip.paste()\n",
    "    print(\"mcbShelf[sys.argv[2]] = \", list(mcbShelf.keys()))\n",
    "    print('Save clipboard content:Succes!')\n",
    "elif len(sys.argv) == 2:\n",
    "    # List keywords and load content.\n",
    "    if sys.argv[1].lower() == 'list':\n",
    "        pyperclip.copy(str(list(mcbShelf.keys())))\n",
    "        print('Load Keywords from clipboard content:Succes!')\n",
    "    elif sys.argv[1] in mcbShelf:\n",
    "        pyperclip.copy(mcbShelf[sys.argv[1]])\n",
    "        print('Load Keyword from clipboard content:Succes!')\n",
    "print('\\033[31m{}\\033[0m'.format(list(mcbShelf.keys())))\n",
    "mcbShelf.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[41m list(mcbShelf.keys()) = \u001b[0m ['1234', 'abcd', 'one time one thing']\n",
      "1234  :  mcbShelf\n",
      "abcd  :  # save clipboard content to shelve\n",
      "one time one thing  :  save clipboard content to shelve\n"
     ]
    }
   ],
   "source": [
    "# read content from shelve\n",
    "mcbShelf = shelve.open('mcb')\n",
    "print(\"\\033[41m list(mcbShelf.keys()) = \\033[0m\", list(mcbShelf.keys()))\n",
    "for key in mcbShelf.keys():\n",
    "    print(key, \" : \", mcbShelf[key])\n",
    "mcbShelf.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 组织文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用 send2trash 模块安全地删除\n",
    "\n",
    " Python 内建的 shutil.rmtree()函数<font color=red>不可恢复</font>地删除文件和文件夹\n",
    " \n",
    " 利用 send2trash，比 Python 常规的删除函数要安全得多，它会将文件夹和文件发送到计算机的<font color=blue>垃圾箱或回收站</font>，而不是永久删除它们"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.makedirs('a/b/c')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints',\n",
       " '1',\n",
       " 'a',\n",
       " 'capitalsquiz1.txt',\n",
       " 'capitalsquiz_answers1.txt',\n",
       " 'dir_objectName.py',\n",
       " 'mcb.bak',\n",
       " 'mcb.dat',\n",
       " 'mcb.dir',\n",
       " 'not exists file',\n",
       " 'pformat_myCats.py',\n",
       " 'PythonProgrammingTaskAutomation.ipynb',\n",
       " 'PythonProgrammingTaskAutomation.md',\n",
       " 'shelve_mydata.bak',\n",
       " 'shelve_mydata.dat',\n",
       " 'shelve_mydata.dir',\n",
       " '__pycache__']"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "send2trash.send2trash('a/')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "179.188px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
